# 小游戏 勇者斗恶龙

# 玩家
# 怪物
# 公主

# N*N 地图
#

# 游戏角色
import random


class GameRole():

    # 初始化
    def __init__(self,name,x,y):
        self.name = name
        self.x = x
        self.y = y

    # 角色移动
    def move(self, d=''):
        ds = ['w','a','s','d']
        if d=='':
            d = ds[random.randint(0,3)]
        if d == 'w' and self.y > 1:
            # print(self.name+"向上走")
            self.y -= 1
        elif d=='d' and self.x < 10:
            # print(self.name + "向右走")
            self.x += 1
        elif d=='s' and self.y < 10:
            # print(self.name + "向下走")
            self.y += 1
        elif d=='a' and self.x > 1:
            # print(self.name + "向左走")
            self.x -= 1

    def __str__(self):                                 # 打印输出信息
        return f"{self.name} 坐标{self.x},{self.y}"

class Hero(GameRole):
    def __init__(self,x,y):
        self.name = "英雄"
        self.x = x
        self.y = y

    def move(self):
        d = input("请输入移动的方向:(w向上d向右s向下a向左)：")               # 这里通过子类调用父类的方式实现游戏角色的移动
        super().move(d)


class Monster(GameRole):
    def __init__(self):
        self.name = "怪物"
        self.x = random.randint(1,10)
        self.y = random.randint(1,10)

class Princess(GameRole):
    def __init__(self,x,y):
        self.name = "公主"
        self.x = x
        self.y = y

class Game():

    def __init__(self):
        # 英雄
        self.hero = Hero(1,1)
        # 怪物列表                          # 怪物数量的添加 是在游戏类 中初始值中创建的
        self.monsters = []                # 这里是一个关键点 那就是通过一个列表来创建多个怪物 且列表中的每个元素都代表一个怪物整体
        for i in range(10):
            self.monsters.append(Monster())

        self.princess = Princess(10,10)

    def printMap(self):
        # print(self.monsters)
        for i in range(1,11):
            for j in range(1,11):
                printed = False
                if self.hero.x == j and self.hero.y == i:
                    print("🤴", end="")
                    printed = True
                elif self.princess.x == j and self.princess.y == i:
                    print("‍👸", end="")
                    printed = True
                else:
                    for k in range(10):
                        if self.monsters[k].x == j and self.monsters[k].y == i:
                            print("🐉", end="")
                            printed = True
                            break
                if not printed:                        # 取反，也就是没有被打印的地方打印#号
                    print("⬜",end="")
            print("")
        pass

    def roleMove(self):
        self.hero.move()
        for i in range(10):           #  通过一个循环来让所有的怪物动起来
            self.monsters[i].move()
        self.princess.move()

    def checkWin(self):
        # 判断是否追上公主
        if self.hero.x==self.princess.x and self.hero.y==self.princess.y:
            return 1
        # 判断坐标是否和某个怪物一样
        for i in range(10):
            if self.hero.x==self.monsters[i].x and self.hero.y==self.monsters[i].y:
                return -1
        return 0

    # 游戏主流程
    def play(self):
        while True:
            #1. 打印信息
            self.printMap()
            #2. 角色移动
            self.roleMove()
            #3. 判断输赢
            result = self.checkWin()
            if result==1:
                print("玩家追上了公主,You win!")
                break
            elif result==-1:
                print("你被怪物追上了,You lost!")
                break
        print("游戏结束")


game = Game()
game.play()